package cn.chinaflame.pagodas;

import static cn.chinaflame.pagodas.utils.LightAlgo.LightUtil.AREA01;
import static cn.chinaflame.pagodas.utils.LightAlgo.LightUtil.AREA02;
import static cn.chinaflame.pagodas.utils.LightAlgo.LightUtil.AREA03;
import static cn.chinaflame.pagodas.utils.LightAlgo.LightUtil.AREA04;
import static cn.chinaflame.pagodas.utils.LightAlgo.LightUtil.AREA05;
import static cn.chinaflame.pagodas.utils.LightAlgo.LightUtil.AREA06;
import static cn.chinaflame.pagodas.utils.LightAlgo.LightUtil.AREA07;
import static cn.chinaflame.pagodas.utils.LightAlgo.LightUtil.AREA08;
import static cn.chinaflame.pagodas.utils.LightAlgo.LightUtil.AREA09;
import static cn.chinaflame.pagodas.utils.LightAlgo.LightUtil.AREA10;
import static cn.chinaflame.pagodas.utils.LightAlgo.LightUtil.AREA11;
import static cn.chinaflame.pagodas.utils.LightAlgo.LightUtil.AREA12;
import static cn.chinaflame.pagodas.utils.LightAlgo.LightUtil.AREA13;
import static cn.chinaflame.pagodas.utils.LightAlgo.LightUtil.AREA14;
import static cn.chinaflame.pagodas.utils.LightAlgo.LightUtil.SEAT;
import static cn.chinaflame.pagodas.utils.LightAlgo.LightUtil.areas;
import static cn.chinaflame.pagodas.utils.LightAlgo.LightUtil.pillarMap1;
import static cn.chinaflame.pagodas.utils.LightAlgo.LightUtil.pillarMap10;
import static cn.chinaflame.pagodas.utils.LightAlgo.LightUtil.pillarMap11;
import static cn.chinaflame.pagodas.utils.LightAlgo.LightUtil.pillarMap12;
import static cn.chinaflame.pagodas.utils.LightAlgo.LightUtil.pillarMap13;
import static cn.chinaflame.pagodas.utils.LightAlgo.LightUtil.pillarMap14;
import static cn.chinaflame.pagodas.utils.LightAlgo.LightUtil.pillarMap2;
import static cn.chinaflame.pagodas.utils.LightAlgo.LightUtil.pillarMap3;
import static cn.chinaflame.pagodas.utils.LightAlgo.LightUtil.pillarMap4;
import static cn.chinaflame.pagodas.utils.LightAlgo.LightUtil.pillarMap5;
import static cn.chinaflame.pagodas.utils.LightAlgo.LightUtil.pillarMap6;
import static cn.chinaflame.pagodas.utils.LightAlgo.LightUtil.pillarMap7;
import static cn.chinaflame.pagodas.utils.LightAlgo.LightUtil.pillarMap8;
import static cn.chinaflame.pagodas.utils.LightAlgo.LightUtil.pillarMap9;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.chinaflame.pagodas.dto.AreaDto;
import cn.chinaflame.pagodas.entity.PaProductArea;
import cn.chinaflame.pagodas.service.PaProductAreaService;
import cn.chinaflame.pagodas.service.PaProductPositionService;
import cn.chinaflame.pagodas.service.YwAreaService;
import cn.chinaflame.pagodas.spring.ApplicationContextHolder;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import com.alibaba.druid.pool.DruidDataSource;

import cn.chinaflame.pagodas.aop.WebHandleInterceptorAdvisor;
import static cn.chinaflame.pagodas.constant.Sysconst.AREA_TYPE_PROVINCE;
import static cn.chinaflame.pagodas.constant.Sysconst.ProvinceList;
import static cn.chinaflame.pagodas.constant.Sysconst.CityList;
import static cn.chinaflame.pagodas.constant.Sysconst.AREA_TYPE_COUNTY;
import static cn.chinaflame.pagodas.constant.Sysconst.CountyList;
import static cn.chinaflame.pagodas.constant.Sysconst.AREAS;
import static cn.chinaflame.pagodas.constant.Sysconst.AREA_TYPE_CITY;
/**
 * @author admin
 * @SpringBootApplication 启动注解
 * @EnableScheduling 支持定时任务的注解
 */
@SpringBootApplication
@EnableScheduling
public class PagodasApplication {
	
	@Value("${ds.jdbcurl}")
	private String jdbcurl;
	@Value("${ds.usrname}")
	private String usrname;
	@Value("${ds.passwd}")
	private String passwd;
	
	
//	@Value("${pagodas.jdbcurl}")
//	private String pagodasjdbcurl;
//	@Value("${pagodas.usrname}")
//	private String pagodasusrname;
//	@Value("${pagodas.passwd}")
//	private String pagodaspasswd;
	
	
	@Bean
	public DataSource getDataSource() {
		DruidDataSource dataSource = new DruidDataSource();
		dataSource.setDriverClassName("com.mysql.jdbc.Driver");
//		dataSource.setUrl(jdbcurl);
//		dataSource.setUsername(usrname);
//		dataSource.setPassword(passwd);
		dataSource.setUrl(jdbcurl);//"jdbc:mysql://192.168.1.38:3306/pagoda?useAffectedRows=true&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&rewriteBatchedStatements=true"
		dataSource.setUsername(usrname);//"root"
		dataSource.setPassword(passwd);//"yw@123$%^"
//		dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/pagoda1?useAffectedRows=true");
//		dataSource.setUsername("root");
//		dataSource.setPassword("");
//		dataSource.setUrl("jdbc:mysql://192.168.1.24:3306/pagoda?useAffectedRows=true");
//		dataSource.setUsername("root");
//  		dataSource.setPassword("yw@123$%^");
		dataSource.setValidationQuery("SELECT 1");
		dataSource.setTestWhileIdle(true);
		dataSource.setTestOnBorrow(true);
		dataSource.setTestOnBorrow(false);
		dataSource.setDefaultAutoCommit(true);
		dataSource.setTimeBetweenEvictionRunsMillis(300000);
		dataSource.setNumTestsPerEvictionRun(50);
		dataSource.setInitialSize(8);
		dataSource.setRemoveAbandoned(true);
		dataSource.setRemoveAbandonedTimeout(1800);
		dataSource.setLogAbandoned(true);
		dataSource.setMinEvictableIdleTimeMillis(3600000);
		return dataSource;
	}
    public static void main(String[] args) {
    	SpringApplication.run(PagodasApplication.class, args);
        init();
    }
    
    /**
     * 加载省市区域位置，灯的所有区号和位置
     */
    private static void init() {
        YwAreaService ywAreaService = ApplicationContextHolder.getBean(YwAreaService.class);
        List<AreaDto> list = ywAreaService.queryList(null, null, null);
        for (int i = 0; i < list.size(); i++) {
            if (AREA_TYPE_PROVINCE.equals(list.get(i).getType()))
                ProvinceList.put(list.get(i).getCode(), list.get(i).getName());
            else if (AREA_TYPE_CITY.equals(list.get(i).getType()))
                CityList.put(list.get(i).getCode(), list.get(i).getName());
            else if (AREA_TYPE_COUNTY.equals(list.get(i).getType()))
                CountyList.put(list.get(i).getCode(), list.get(i).getName());
            else {
            }
//				throw new HandlerException(CodeEnum.FAIL.getCode(), "错误的省市区类型");
        }
        PaProductAreaService paProductAreaService = ApplicationContextHolder.getBean(PaProductAreaService.class);
        paProductAreaService.loadProductAreas();	//加载所有区号
        PaProductPositionService paProductPositionService = ApplicationContextHolder.getBean(PaProductPositionService.class);
        paProductPositionService.loadProductPositions();  //加载所有位置
    }
    
    
    @Bean
    public WebHandleInterceptorAdvisor webHandleInterceptorAdvisor() {
        return new WebHandleInterceptorAdvisor();
    }

    @Bean
    RestTemplate restTemplate(RestTemplateBuilder builder) {
    	// Do any additional configuration here
        return builder.build();
    }
     
	
	@Bean
	public FilterRegistrationBean corsFilter() {
		UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
		CorsConfiguration config = new CorsConfiguration();
		config.setAllowCredentials(true);
		config.addAllowedOrigin("*");
		config.addAllowedHeader("*");
		config.addAllowedMethod("*");
		config.addExposedHeader("x-auth-token");
		config.addExposedHeader("x-total-count");
		source.registerCorsConfiguration("/**", config);
		FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
		bean.setOrder(0);
		return bean;
	}
	
}
